
import 'dart:math';

import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/KugouSong.dart';
import 'package:freemusic/services/SongServices.dart';

enum PlayModel{
  single,     //单曲循环
  loopall,    //列表循环
  shuffle,    //随机播放
}

/*
  如果播放的歌曲为当前歌单，则songList 不变
  如果即将播放的歌曲为其他歌单，则追加到 songList中
 */
class PlayerStore extends Store{

  AbstractSong _current = KugouSong();               //当前播放的歌曲
  bool _playing = false;                             //播放状态
  PlayModel _playModel = PlayModel.loopall;
  Duration _position = Duration(milliseconds: 0);    // 当前播放到的时间
  List<AbstractSong> _songList = List();             // 当前播放的所有歌曲


  PlayerStore(){
    triggerOnAction(playSong, (AbstractSong song){

      if(current !=null && current.hash == song.hash){
        return ;
      }
      _current = song;
      _playing = true;
      int index = songList.indexWhere((item)=> item.hash == song.hash);
      if(index == -1){
        songList.add(song);
      }
      return;
    });
    triggerOnAction(changePlaying, (bool paly){
      this._playing = paly;
    });
    triggerOnAction(nextPlay, (v){
      if (_playModel == PlayModel.shuffle){
        _shufflePaly();
        return ;
      }
      _nextPaly();
      return;
    });
    triggerOnAction(previousPlay, (v){
      _prevPlay();
    });
    triggerOnAction(changeSongSheet, (List<AbstractSong> songs){
      _songList = songs;
      if(songs.length == 0){
        return ;
      }
      _playing = true;
      _changePlaySong(0);
    });
    triggerOnAction(deleteSong, (AbstractSong song){
      int index = songList.indexWhere((item)=> item.hash == song.hash);
      if(index == -1){
        return ;
      }
      songList.removeAt(index);
    });
    triggerOnAction(completPlay, (dynamic param){
      if(_playModel == PlayModel.single){
        SongServices.getInstace().playSong(current);
      }else if (_playModel == PlayModel.shuffle){
        _shufflePaly();
      }else {
        _nextPaly();
      }
    });
    triggerOnAction(changePlayModel, (dynamic param){
      PlayModel currentModel = _playModel;
      if(currentModel == PlayModel.loopall){
        _playModel = PlayModel.shuffle;
      }else if( currentModel == PlayModel.shuffle){
        _playModel = PlayModel.single;
      }else {
        _playModel = PlayModel.loopall;
      }
    });
    triggerOnAction(addNextPlay, (AbstractSong song){
      int index = _songList.indexWhere((item) => item.hash == song.hash);
      if(index != -1 ){
        return ;
      }
      index = _songList.indexWhere((item) => item.hash == _current.hash);
      _songList.insert(index +1 , song);
    });
    triggerOnAction(initSongSheet, (List<AbstractSong> songs){
      _songList = songs;
    });
  }

  //随机播放
  void _shufflePaly(){
    int currentIndex = songList.indexWhere((item)=> item.hash == current.hash);
    int count = _songList.length;
    int index = Random().nextInt(count);
    if(currentIndex == index){
      index ++;
    }
    _changePlaySong(index);
  }
  //播放下一首
  void _nextPaly(){
    int index = songList.indexWhere((item)=> item.hash == current.hash);
    index = index + 1;
    _changePlaySong(index);
  }
  void _prevPlay(){
    int index = songList.indexWhere((item)=> item.hash == current.hash);
    _changePlaySong(--index);
  }
  void _changePlaySong(int index){
    int count = songList.length;
    //如果最后一首播放完毕，切换第一首播放
    if(index >= count){
      index = 0;
    }else if( index < 0){
      index = count -1;
    }
    if(songList.length == 0){
      return;
    }
    _current = songList[index];
    this._playing = true;
    SongServices.getInstace().playSong(current);
  }

  AbstractSong get current => _current;
  bool get playing => _playing;
  Duration get position => _position;
  List<AbstractSong> get songList => _songList;
  PlayModel get playModel => _playModel;
}

StoreToken playerStoreToken = StoreToken(PlayerStore());

final Action<List<AbstractSong>> initSongSheet = Action();
final Action<AbstractSong> playSong = Action();                     //播放歌曲
final Action<bool> changePlaying = Action();                             //停止播放
final Action<List<AbstractSong>> changeSongSheet = Action();        //更换歌单
final Action nextPlay = Action();                                   //下一首
final Action previousPlay = Action();                               //上一首
final Action<AbstractSong> deleteSong = Action();
final Action completPlay = Action();                                //歌曲播放完成后的操作
final Action changePlayModel = Action();                            //改变播放模式
final Action<AbstractSong> addNextPlay = Action();                  //添加到下一首播放

